package com.xiansi.srb.core.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xiansi.srb.core.mapper.DictMapper;
import com.xiansi.srb.core.pojo.dto.ExcelDictDTO;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@NoArgsConstructor
public class ExcelDictDTOListener extends AnalysisEventListener<ExcelDictDTO> {

    private DictMapper dictMapper;

    private List<ExcelDictDTO> list = new ArrayList<ExcelDictDTO>();

    private static final int BATCH_COUNT = 5;

    public ExcelDictDTOListener(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }

    @Override
    public void invoke(ExcelDictDTO data, AnalysisContext context) {
        log.info("解析到一条数据：{}", data);

        //将数据存入数据列表
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }


    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        //当最后剩余的数据记录数不足BATCH-COUNT时，最终一次存储剩余数据
        saveData();
        log.info("数据解析完成");
    }

    private void saveData() {
        log.info("{} 条数据被存储到数据库", list.size());
        //调用mapper层的save方法：save list 对象
       dictMapper.insetBatch(list);
        log.info("{} 条数据被存储到数据库成功", list.size());
    }
}
